package com.richman.hg;

import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import com.richman.controller.BetClient;
import com.richman.ps38.PS38Http;
import com.richman.ps38.entity.BetAdyData;
import com.richman.ps38.entity.HGEvent;
import com.richman.ps38.entity.RedcardsRecords;

public class HGManager {
	
	public static Vector<RedcardsRecords> redcardsRecords = new Vector<RedcardsRecords>(); 
	
	public int usedAccountIndex = 0;
	
	//HGHttp account = null;
	
	Vector<HGHttpSpecial> specialAccounts = new Vector<HGHttpSpecial>();
	
	Vector<HGHttp> normalAccounts = new Vector<HGHttp>();

	
	Vector<String> accountnames = new Vector<String>();
	
	public void useNextAccount() {
		usedAccountIndex += 1;
		
		if(BetClient.betType == 0) {
			if(specialAccounts.size() <= usedAccountIndex) {
				usedAccountIndex = 0;
			}
		}else {
			if(normalAccounts.size() <= usedAccountIndex) {
				usedAccountIndex = 0;
			}
		}
		
		
	}
	
	public String getinUsedAccountName() {
		//return accountnames.elementAt(usedAccountIndex+1);
		return accountnames.elementAt(usedAccountIndex);
	}
	
	public void init() {
		
		Vector<String[]> tmps = BetClient.mainWnd.getHGAccounts();

		if(tmps.size() > 0) {
			
			if(BetClient.betType == 0) {
				for(int i = 0; i < tmps.size(); i++) {
					HGHttpSpecial hghttp = new HGHttpSpecial();
					hghttp.setLoginParams(tmps.elementAt(i)[0], tmps.elementAt(i)[1],
							tmps.elementAt(i)[2]);
					accountnames.add(tmps.elementAt(i)[1]);
					specialAccounts.add(hghttp);
					
				}
			}else if(BetClient.betType == 1) {
				for(int i = 0; i < tmps.size(); i++) {
					
					HGHttp hghttp = new HGHttp();
					hghttp.setLoginParams(tmps.elementAt(i)[0], tmps.elementAt(i)[1],
							tmps.elementAt(i)[2]);
					accountnames.add(tmps.elementAt(i)[1]);
					normalAccounts.add(hghttp);

				}

			}
			
			

		}	
		

		BetClient.mainWnd.updateBrowserAccounts(accountnames);
		
	}
	





	
	public void login() {
		
		if(BetClient.betType == 0) {
			if(specialAccounts.size() > 0) {
				
				for(int i = 0; i < specialAccounts.size(); i++) {
					
					if(i==usedAccountIndex) {
						if(specialAccounts.elementAt(i).getStatusCode() == -1) {
							specialAccounts.elementAt(i).login();
						}
						if((specialAccounts.elementAt(i).getStatusCode() == -2) && (System.currentTimeMillis() > specialAccounts.elementAt(i).getCanLoginTime())) {
							specialAccounts.elementAt(i).login();
						}
					}
					

				}
				
			}
		}else if(BetClient.betType == 1) {			
			if(normalAccounts.size() > 0) {
				
				for(int i = 0; i < normalAccounts.size(); i++) {
					if(normalAccounts.elementAt(i).getStatusCode() == -1) {
						normalAccounts.elementAt(i).login();
					}
					if((normalAccounts.elementAt(i).getStatusCode() == -2) && (System.currentTimeMillis() > normalAccounts.elementAt(i).getCanLoginTime())) {
						normalAccounts.elementAt(i).login();
					}
				}
				
			}
		}

		
	}
	

	public void checkIsOnline() {
		
		if( normalAccounts.size() > 0) {
			
			for(int i = 0; i < normalAccounts.size(); i++) {
				if((normalAccounts.elementAt(i).getStatusCode() == 1) && (System.currentTimeMillis() > normalAccounts.elementAt(i).getCheckOnlineTime())) {
					normalAccounts.elementAt(i).checkIsOnline();
				}
			}
			
			
		}

		
		if( specialAccounts.size() > 0) {
			
			for(int i = 0; i < specialAccounts.size(); i++) {
				if(i==usedAccountIndex) {
					if((specialAccounts.elementAt(i).getStatusCode() == 1) && (System.currentTimeMillis() > specialAccounts.elementAt(i).getCheckOnlineTime())) {
						specialAccounts.elementAt(i).checkIsOnline();
					}
				}

			}
			
			
		}
		
	}


	public Map<String, String> updateRatio(String gid, String type, String gnum, String strong, String rtype,
			String wtype) {

		
		if(normalAccounts.size() > usedAccountIndex) {
			return normalAccounts.elementAt(usedAccountIndex).updateRatio(gid, type, gnum, strong, rtype, wtype);
		}
		return null;
	}

	public Map<String, String> updateSpecialAccountRatio(String gid, String type, String gnum, String strong, String rtype,
			String wtype) {
		
		if(specialAccounts.size() > usedAccountIndex) {
			return specialAccounts.elementAt(usedAccountIndex).updateRatio(gid, type, gnum, strong, rtype, wtype);
		}
		return null;
		

	}

	

	public int getStatuscode() {
		if(normalAccounts.size() > usedAccountIndex) {
			return normalAccounts.elementAt(usedAccountIndex).getStatusCode();
		}
		
		return -1;
	}

	public int getSpecialAccountStatuscode() {
		
		if(specialAccounts.size() > usedAccountIndex) {
			return specialAccounts.elementAt(usedAccountIndex).getStatusCode();
		}
		
		return -1;

	}

	public Map<String,String> dobet(String betParam, int gold) {
		if(normalAccounts.size() > usedAccountIndex) {
			return normalAccounts.elementAt(usedAccountIndex).doBet(betParam, gold);
		}
		return null;
	}
	
	public Map<String,String> dobetSpecialaccount(String betParam, int gold) {
		
		if(specialAccounts.size() > usedAccountIndex) {
			return specialAccounts.elementAt(usedAccountIndex).doBet(betParam, gold);
		}
		return null;

	}
	
	public String getShowTodayRecURL(int index) {
		
		if(normalAccounts.size() > index) {
			return normalAccounts.elementAt(index).getShowTodayRecURL();
		}
		return null;
		
	}

	public String getSpecialAccountShowTodayRecURL(int index) {
		if(specialAccounts.size() > index) {
			return specialAccounts.elementAt(index).getShowTodayRecURL();
		}
		return null;
	}

	
	public String getUser_id(int index) {
		if(normalAccounts.size() > index) {
			return normalAccounts.elementAt(index).getuser_id();
		}
		return null;
		
	
	}

	public String getSpecialAccountUser_id(int index) {
		if(specialAccounts.size() > index) {
			return specialAccounts.elementAt(index).getuser_id();
		}
		return null;
	}

	
	public String getHistoryRecURL(int index) {
		if(normalAccounts.size() > index) {
			return normalAccounts.elementAt(index).getHistoryRecURL();
		}
		return null;

	}

	public String getSpecialAccountHistoryRecURL(int index) {
		if(specialAccounts.size() > index) {
			return specialAccounts.elementAt(index).getHistoryRecURL();
		}
		return null;
	}

	

	public void updatehgBetLogData(Vector<BetAdyData> betLogDatas) {
		if(normalAccounts.size() > usedAccountIndex) {
			if(normalAccounts.elementAt(usedAccountIndex).getStatusCode() == 1) {
				normalAccounts.elementAt(usedAccountIndex).updatehgBetLogData(betLogDatas);
			}
			
		}

	}

	
	public void updatehgSpecialAccountBetLogData(Vector<BetAdyData> betLogDatas) {

		if(specialAccounts.size() > usedAccountIndex && specialAccounts.elementAt(usedAccountIndex).getStatusCode() == 1) {
			specialAccounts.elementAt(usedAccountIndex).updatehgBetLogData(betLogDatas);
		}
		
	}


	public void checkIllegalBet() {

		for(int i = 0 ;i <normalAccounts.size(); i++ ) {
			if(normalAccounts.elementAt(i) != null && normalAccounts.elementAt(i).getStatusCode() == 1) {
				normalAccounts.elementAt(i).checkIllealBet();
			}
		}
		
	}

	public void checkSpecialIllegalBet() {

		
		for(int i = 0 ;i <specialAccounts.size(); i++ ) {
			if(specialAccounts.elementAt(i) != null && specialAccounts.elementAt(i).getStatusCode() == 1) {
				specialAccounts.elementAt(i).checkIllealBet();
			}
		}

	}

	

	public String getAccounttype() {
		if(normalAccounts.size() > usedAccountIndex) {
			return	normalAccounts.elementAt(usedAccountIndex).getmtype();
		}
		return "3";
	}

	
	public String getSpecialAccounttype() {
//		if(specialAccount != null ) {
//			return specialAccount.getmtype();
//		}
		if(specialAccounts.elementAt(usedAccountIndex) != null ) {
			return specialAccounts.elementAt(usedAccountIndex).getmtype();
		}
		
		return "3";
	}


	public Map<String, String> getInplaybetScores() {
		
		if(normalAccounts.size() > usedAccountIndex) {
			if(normalAccounts.elementAt(usedAccountIndex).getStatusCode() == 1) {
				return normalAccounts.elementAt(usedAccountIndex).getInplaybetScores();
			}

		}

		
		return null;
	}
	
	public void getRedCard() {
		
		if(normalAccounts.size() > usedAccountIndex) {
			if(normalAccounts.elementAt(usedAccountIndex).getStatusCode() == 1) {
				normalAccounts.elementAt(usedAccountIndex).getInplaybet();
			}

		}

	}


	public Vector<HGEvent> getSpecialAccountInplaybet() {

		if(specialAccounts.elementAt(usedAccountIndex) != null ) {
			return specialAccounts.elementAt(usedAccountIndex).getInplaybet();
		}
		
		return null;
	}

	public String getHGBetAccount() {
		if(specialAccounts.elementAt(usedAccountIndex) != null ) {
			return specialAccounts.elementAt(usedAccountIndex).getAccount();
		}
		
		return null;
		
	}
	
}
